package edu.pku.taas.hyperic.monitor;

import java.util.Properties;

import org.apache.log4j.Level;
import org.apache.log4j.PropertyConfigurator;

public class LogConfigure {
	private static boolean _logConfigured = false;

	// Ripped out from PluginMain.java
	private static final String[][] LOG_PROPS = {
			{ "log4j.appender.R", "org.apache.log4j.ConsoleAppender" },
			{ "log4j.appender.R.layout.ConversionPattern", "%-5p [%t] [%c{1}] %m%n" },
			{ "log4j.appender.R.layout", "org.apache.log4j.PatternLayout" },
			{ "log4j.appender.file", "org.apache.log4j.DailyRollingFileAppender" },
			{ "log4j.appender.file.File", "E:/monitor-log/monitor.log" },
			// { "log4j.appender.file.MaxFileSize","2MB" },
			// { "log4j.appender.file.MaxBackupIndex","1" },
			{ "log4j.appender.file.DatePattern", "'.'yyyy-MM-dd'.log'" },
			{ "log4j.appender.file.layout", "org.apache.log4j.PatternLayout" },
			{ "log4j.appender.file.layout.ConversionPattern", "%d  %-5p (%F:%L) - %m%n " } };

	private static void configureLogging(String level) {
		Properties props = new Properties();
		props.setProperty("log4j.rootLogger", level.toUpperCase() + ", R,file");
		// props.setProperty("log4j.logger.httpclient.wire",
		// level.toUpperCase());
		// props.setProperty("log4j.logger.org.apache.commons.httpclient", level
		// .toUpperCase());
		for (String[] PROPS : LOG_PROPS) {
			props.setProperty(PROPS[0], PROPS[1]);
		}

		props.putAll(System.getProperties());
		PropertyConfigurator.configure(props);
	}

	public static void setUp() {
		if (!_logConfigured) {
//			String level = System.getProperty("log");
			String level = MonitorConfigureManager.getStringConfigure(MonitorConstants.ROOT_LOG_LEVEL);
			System.out.println("log level ====="+level);
			if (level != null) {
				configureLogging(level);
			} else {
				configureLogging("INFO");
				//configureLogging("DEBUG");
			}
		}
	}

}
